//
//  SEOrderExamineCell.swift
//  SmartEyeForPatient
//
//  Created by QDSG on 2021/4/16.
//

import UIKit

protocol SEOrderExamineCellDelegate: NSObjectProtocol {
    func orderExamineCell(_ cell: SEOrderExamineCell, didClickedDetailWith viewModel: SEExamineDetailViewModel)
    func orderExamineCell(_ cell: SEOrderExamineCell, didClickedPayWith viewModel: SEExamineDetailViewModel)
    func orderExamineCell(_ cell: SEOrderExamineCell, didClickedCancelWith viewModel: SEExamineDetailViewModel)
}

class SEOrderExamineCell: UITableViewCell {
    
    weak var delegate: SEOrderExamineCellDelegate?
    var viewModel = SEExamineDetailViewModel()

    override func awakeFromNib() {
        super.awakeFromNib()
        backgroundColor = .white
        contentView.backgroundColor = .white
    }
    
    func update(with viewModel: SEExamineDetailViewModel) {
        self.viewModel = viewModel
        hospitalLabel.text = viewModel.model.checkHosName
        nameLabel.text = viewModel.model.name
        timeLabel.text = viewModel.model.checkScheduleTime
        collectionView.titles = viewModel.checkItemNamesArray
        statusLabel.text = viewModel.checkStatus.title
        statusLabel.textColor = viewModel.checkStatus.titleColor
        payButton.isHidden = viewModel.isHiddenPayBtn
        cancelButton.isHidden = viewModel.isHiddenCancelBtn
        detailButton.isHidden = viewModel.isHiddenDetailBtn

        frame = UIScreen.main.bounds
        layoutIfNeeded()
        collectionView.reloadData()
        let contentSize = collectionView.collectionViewLayout.collectionViewContentSize
        collectionViewHeightConstraint.constant = contentSize.height
        infoViewTopConstraint.priority = contentSize.height == 0 ? .defaultHigh : .defaultLow
    }
    
    @IBAction private func detailAction() {
        delegate?.orderExamineCell(self, didClickedDetailWith: self.viewModel)
    }
    
    @IBAction private func payAction() {
        delegate?.orderExamineCell(self, didClickedPayWith: self.viewModel)
    }
    
    @IBAction private func cancelAction() {
        delegate?.orderExamineCell(self, didClickedCancelWith: self.viewModel)
    }

    @IBOutlet private var collectionViewHeightConstraint: NSLayoutConstraint!
    @IBOutlet private var infoViewTopConstraint: NSLayoutConstraint!
    
    @IBOutlet private var infoView: UIView! {
        didSet {
            infoView.layer.cornerRadius = 4
            infoView.layer.borderColor = Asset.main.color.withAlphaComponent(0.15).cgColor
            infoView.layer.borderWidth = 1
        }
    }
    
    @IBOutlet private weak var hospitalLabel: UILabel!
    @IBOutlet private weak var statusLabel: UILabel!
    @IBOutlet private weak var nameLabel: UILabel!
    @IBOutlet private weak var timeLabel: UILabel!
    @IBOutlet private weak var detailButton: UIButton! {
        didSet {
            detailButton.layer.cornerRadius = detailButton.height / 2
            detailButton.layer.borderWidth = 1
            detailButton.layer.borderColor = detailButton.titleLabel?.textColor.cgColor
        }
    }
    @IBOutlet private weak var payButton: UIButton! {
        didSet {
            payButton.layer.cornerRadius = payButton.height / 2
            payButton.layer.borderWidth = 1
            payButton.layer.borderColor = payButton.titleLabel?.textColor.cgColor
        }
    }
    @IBOutlet private weak var cancelButton: UIButton! {
        didSet {
            cancelButton.layer.cornerRadius = cancelButton.height / 2
            cancelButton.layer.borderWidth = 1
            cancelButton.layer.borderColor = cancelButton.titleLabel?.textColor.cgColor
        }
    }
    
    @IBOutlet private weak var collectionView: SETagsCollectionView!
}
